// pages/search/search.js
import request from '../../util/request'
let isSend = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholderContent: '', //搜索默认内容
    hotList: [], //热搜榜数据列表
    searchContent: '', //用户输入的内容
    searchList: [], //搜索数据列表
    historyList: [], //历史搜索记录
    content: '', //用于传输的内容
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getInitData();
    this.getSearchHistoryList()
  },
  // 获取初始化数据
  async getInitData() {
    //获取搜索框默认内容
    let placeholderContent = await request('/search/default')
    let hotListData = await request('/search/hot/detail')
    this.setData({
      placeholderContent: placeholderContent.data.showKeyword,
      hotList: hotListData.data
    })
  },
  //获取本地搜索的历史记录
  getSearchHistoryList() {
    let historyList = wx.getStorageSync('searchHistoryList')
    if (historyList) {
      this.setData({
        historyList
      })
    }
  },
  // 表单项内容发生改变的回调
  handleInputChange(event) {
    this.setData({
      searchContent: event.detail.value.trim(),
    })
    if (isSend) {
      return
    }
    isSend = true
    this.getSearchList()
    setTimeout(() => {
      isSend = false
    }, 300)
  },
  // 获取搜索列表
  async getSearchList() {
    if (!this.data.searchContent) {
      this.setData({
        searchList: []
      })
      return
    }
    // let {
    //   searchContent,
    //   historyList
    // } = this.data
    let searchListData = await request('/search', {
      keywords: this.data.searchContent,
      limit: 10
    })
    this.setData({
      searchList: searchListData.result.songs
    })
    // // 将搜索的关键字添加到历史数组中
    // if (historyList.indexOf(searchContent) !== -1) {
    //   historyList.splice(historyList.indexOf(searchContent), 1)
    // }
    // historyList.unshift(searchContent)
    // this.setData({
    //   historyList
    // })
    // wx.setStorageSync('searchHistoryList', historyList)
  },
  //清空搜索框的关键字
  clearSearchContent() {
    this.setData({
      searchContent: '',
      searchList: []
    })
  },
  //清空搜索的历史记录
  deletSearcheHistory() {
    wx.showModal({
      content: '确定清空全部历史记录？',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            historyList: []
          })
          wx.removeStorageSync('searchHistoryList')
        }
      }
    })
  },
  // 搜索
  search() {
    let {
      searchContent,
      historyList
    } = this.data
    // 将搜索的关键字添加到历史数组中
    if (historyList.indexOf(searchContent) !== -1) {
      historyList.splice(historyList.indexOf(searchContent), 1)
    }
    historyList.unshift(searchContent)
    this.setData({
      historyList,
      content: searchContent
    })
    wx.setStorageSync('searchHistoryList', historyList)
    this.toSearchDetail()
  },

  //搜索列表的历史记录
  searchName(event) {
    let name = event.currentTarget.dataset.name
    let {
      historyList
    } = this.data
    // 将搜索的关键字添加到历史数组中
    if (historyList.indexOf(name) !== -1) {
      historyList.splice(historyList.indexOf(name), 1)
    }
    historyList.unshift(name)
    this.setData({
      historyList,
      content: name
    })
    wx.setStorageSync('searchHistoryList', historyList)
    this.toSearchDetail()
  },
  //跳转到搜索详情
  toSearchDetail(){
  //路由跳转传参 query参数
  wx.navigateTo({
    // url: '/pages/songDetail/songDetail?song='+JSON.stringify(song),
    url: '/pages/searchDetail/searchDetail?content=' + this.data.content


  })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})